package com.andy.nature.ui.adapter

import android.content.Context
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.andy.common.model.Label
import com.andy.common.model.Work
import com.andy.nature.databinding.AdapterChildItemBinding
import com.andy.toast.extend.colorFromRes

class TotalLabelAdapter : RecyclerView.Adapter<TotalLabelAdapter.ItemViewHolder>() {
    private var mContext: Context? = null
    private var label: Label? = null
    private var workList = mutableListOf<Work?>()
    private var labelList: MutableList<Label> = mutableListOf()

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        mContext = recyclerView.context
    }

    fun setData(label: Label?, workList: List<Work>?, labelList: List<Label>?) {
        this.label = label
        this.workList.clear()
        this.workList.add(null)
        if (!workList.isNullOrEmpty()) {
            this.workList.addAll(workList)
        }
        this.labelList.clear()
        if (!labelList.isNullOrEmpty()) {
            this.labelList.addAll(labelList)
        }
        notifyDataSetChanged()
    }


    override fun getItemCount(): Int {
        return workList.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        val binding = AdapterChildItemBinding.inflate(LayoutInflater.from(mContext), parent, false)
        return ItemViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        holder.binding.apply {
            if (position == 0) {
                tvQymc.text = "企业名称"
                tvZpyq.text = "招聘要求"
                tvHyzc.text = "会员政策"
                tvGwbq.text = "岗位标签"
                tvEwsr.text = "额外收入"
                tvQymc.setTextColor(com.andy.base.R.color.color_FF0000.colorFromRes())
                tvZpyq.setTextColor(com.andy.base.R.color.color_FF0000.colorFromRes())
                tvHyzc.setTextColor(com.andy.base.R.color.color_FF0000.colorFromRes())
                tvGwbq.setTextColor(com.andy.base.R.color.color_FF0000.colorFromRes())
                tvEwsr.setTextColor(com.andy.base.R.color.color_FF0000.colorFromRes())
                tvQymc.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 8f)
                tvZpyq.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 8f)
                tvHyzc.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 8f)
                tvGwbq.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 8f)
                tvEwsr.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 8f)
            } else {
                val data = workList[position]
                tvQymc.text = data?.name ?: ""
                val z = data?.demand?.trim()?.replace("<p>", "")?.replace("</p>", "")?.replace("✔", "")
                val p = z?.replace("男女不限", "")?.replace("?", "")?.replace("年龄", "")?.replace("✅","")
                val y = p?.replace("：", "")?.replace(":", "")?.replace(",", "")?.replace("，", "")
                val q = y?.replace("要求", "")?.replace("性", "")?.replace("周", "")
                tvZpyq.text = q
                tvHyzc.text = "${data?.labour ?: ""} ${data?.subsidize ?: ""}"
                tvGwbq.text = assembleLabelList(data?.label_arr)
                tvEwsr.text = data?.cj_ewsr ?: ""
                tvQymc.setTextColor(com.andy.base.R.color.color_1D1C1E.colorFromRes())
                tvZpyq.setTextColor(com.andy.base.R.color.color_1D1C1E.colorFromRes())
                tvHyzc.setTextColor(com.andy.base.R.color.color_1D1C1E.colorFromRes())
                tvGwbq.setTextColor(com.andy.base.R.color.color_1D1C1E.colorFromRes())
                tvEwsr.setTextColor(com.andy.base.R.color.color_1D1C1E.colorFromRes())
                tvQymc.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 6f)
                tvZpyq.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 6f)
                tvHyzc.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 6f)
                tvGwbq.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 6f)
                tvEwsr.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 6f)
            }
        }
    }

    class ItemViewHolder(val binding: AdapterChildItemBinding) : RecyclerView.ViewHolder(binding.root)

    private fun assembleLabelList(ids: List<String>?): String {
        if (ids.isNullOrEmpty()) {
            return ""
        }
        val labels = StringBuilder()
        labelList.forEach {
            if (ids.contains(it.id)) {
                labels.append(it.name)
                labels.append(",")
            }
        }
        return labels.toString()
    }
}